package com.ai.controller;

import com.ai.domain.pojo.Chat;
import com.ai.domain.vo.ChatVo;
import com.ai.domain.vo.HistoryVo;
import com.ai.service.IChatService;
import com.common.domain.pojo.Result;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;

import java.util.List;

/**
 * <p>
 * 对话列表 前端控制器
 * </p>
 *
 * @author 小杰大人
 * @since 2025-05-08
 */
@RestController
@RequestMapping("/api/ai/chat")
@RequiredArgsConstructor
public class ChatController {

    private final IChatService iChatService;

    // 获取对话列表
    @GetMapping("/history")
    public Result<List<ChatVo>> historyList(){
        return Result.success(iChatService.historyList());
    }

    // 根据会话id查询会话记录
    @GetMapping("/history/{chatId}")
    public Result<List<HistoryVo>> getHistory(@PathVariable("chatId") String chatId){
        return Result.success(iChatService.querySelectByConversationId(chatId));
    }

    // 流式输出
    @GetMapping(value = "/stream" , produces = "text/event-stream;charset=utf-8")
    public Flux<String> stream(
            @RequestParam("prompt") String prompt ,
            @RequestParam("chatId") Long chatId){
        return iChatService.stream("chat", prompt, chatId);
    }

    // 新增对话
    @PostMapping("/add")
    public Result<String> add(){
        iChatService.addChat();
        return Result.success();
    }
    // 删除对话
    @DeleteMapping("/delete/{id}")
    public Result<String> deleteConversation(@PathVariable("id") Long id){
        iChatService.deleteChatById(id);
        return Result.success();
    }
    // 修改对话名称
    @PutMapping("/update")
    public Result<String> update(@RequestBody Chat chat){
        iChatService.updateChat(chat);
        return Result.success();
    }

}
